<?php

define(C("ACCESSACTION"), true);
import('jinghuan.AdminAction');

/**
 * Class ApiAction
 */
class FeedbackAction extends AdminAction
{
    const pwd_str_format = '%s|%s|www.jinghuan.info';

    function datatable()
    {
        $post = I('post.');
        $count = D(JH_FEEDBACK)->where($post["where"]) ->group('name') ->count();
        $row = D(JH_FEEDBACK)
            ->page($post["page"], $post["length"])
            ->where($post["where"])
            ->order('id desc')
            ->group('name')
            ->select();

        $this->callback(array(
            'rows' => $row,
            'echo' => $post["echo"],
            'count' => $count
        ));
    }

    function message_client()
    {
        $post = I('post.');
        if (!$post['where']['account']){
           $_data =  D(JH_FEEDBACK)->field('account')->order('id desc')->find();
           if($_data['account']){
               $post['where']['account']=$_data['account'];
           }
        }
        $post["where"]['msg_type'] = 1;
        $count = D(JH_FEEDBACK)->where($post["where"])->count();
        $row = D(JH_FEEDBACK)
            ->page($post["page"], $post["length"])
            ->where($post["where"])
            ->order('id desc')
            ->select();

        $this->callback(array(
            'rows' => $row,
            'echo' => $post["echo"],
            'count' => $count
        ));
    }

    function message_server()
    {
        $post = I('post.');
        if (!$post['where']['account']){
            $_data =  D(JH_FEEDBACK)->field('account')->order('id desc')->find();
            if($_data['account']){
                $post['where']['account']=$_data['account'];
            }
        }
        $post["where"]['msg_type'] = 0;
        $count = D(JH_FEEDBACK)->where($post["where"])->count();
        $row = D(JH_FEEDBACK)
            ->page($post["page"], $post["length"])
            ->where($post["where"])
            ->order('id desc')
            ->select();

        $this->callback(array(
            'rows' => $row,
            'echo' => $post["echo"],
            'count' => $count
        ));
    }


    /**
     *  添加服务端消息记录
     */
    function do_add()
    {
        $post = I('post.');

        $result = D(JH_FEEDBACK)->add(array(
            "name" => $post['name'],
            "account" => $post['account'],
            "message" => $post['message'],
            "msg_type" => 0,
            "time" => time(),
        ));

        $data = D(JH_FEEDBACK)->find($result);
        $this->callback($data);
    }

    function get_account(){
        $_data =  D(JH_FEEDBACK)->order('id desc')->find();
        $this->callback($_data);
    }

}